package com.daafan.minilib.activity;

import net.tsz.afinal.FinalDb;

import org.apache.log4j.Logger;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.daafan.minilib.R;
import com.daafan.minilib.TushuApplication;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.analytics.MobclickAgent;

public class BaseActivity extends Activity {

	protected Context			mContext;
	protected ImageLoader		mImageLoader	= ImageLoader.getInstance();
	protected Logger			logger			= Logger.getLogger(getClass());
	protected FinalDb			db;

	Button						btn_topLeft;
	Button						btn_topRight;
	TextView					tv_topTitle;

	protected Handler			handler			= new Handler();
	/**
	 * 对话框架对象
	 */
	public AlertDialog			mAlertDialog;
	/**
	 * 对话框架对象mAlertDialogBuilder
	 */
	public AlertDialog.Builder	mAlertDialogBuilder;

	protected ProgressDialog	progressDialog;

	protected float				screenWidth;
	protected float				screenHeight;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = this;
		db = FinalDb.create(getApplicationContext());
		mAlertDialogBuilder = new Builder(this);

		getScreenWH();

		this.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_white));
	}

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void getScreenWH() {
		screenWidth = getWindowManager().getDefaultDisplay().getWidth();
		screenHeight = getWindowManager().getDefaultDisplay().getHeight();
	}

	/**
	 * 初始化顶部按键及标题栏
	 * 
	 * @param title
	 * @param leftBtnBgID
	 *            左键显示图标ID。{@link #LEFT_TOP_BACK}、 {@link #LEFT_TOP_MENU}、 {@link #LEFT_TOP_MESSAGE}.<br>
	 *            如果不显示，传0参数;
	 * 
	 * @param rightBtnBgID
	 *            右键显示图标ID。{@link #RIGHT_TOP_CLOSE}、 {@link #RIGHT_TOP_EDIT}、 {@link #RIGHT_TOP_REFRESH}、 {@link #RIGHT_TOP_SAVE}、
	 *            {@link #RIGHT_TOP_SEARCH}、 {@link #RIGHT_TOP_TEXT}.<br>
	 *            如果不显示，传0参数;
	 * 
	 * @注意 左/右键设置了文本之后，该键就不显示图标了
	 * 
	 */
	public void initTopBar(String title, final int leftBtnBgID, int rightBtnBgID) {
		tv_topTitle = (TextView) findViewById(R.id.tv_topbar_title);
		btn_topLeft = (Button) findViewById(R.id.btn_topbar_left);
		btn_topRight = (Button) findViewById(R.id.btn_topbar_right);

		// 标题
		if (tv_topTitle != null)
			tv_topTitle.setText(title);

		// 左键
		if (btn_topLeft != null) {
			btn_topLeft.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// if (leftBtnBgID == R.drawable.back)
					// finish();

					onTopLeftClick();
				}
			});
			if (leftBtnBgID != 0) {
				btn_topLeft.setVisibility(View.VISIBLE);
				btn_topLeft.setBackgroundDrawable(getResources().getDrawable(leftBtnBgID));
			} else {
				btn_topLeft.setVisibility(View.INVISIBLE);
			}
		}

		// 右键
		if (btn_topRight != null) {
			btn_topRight.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					onTopRightClick();
				}
			});
			if (rightBtnBgID != 0) {
				btn_topRight.setVisibility(View.VISIBLE);
				btn_topRight.setBackgroundDrawable(getResources().getDrawable(rightBtnBgID));
			} else {
				btn_topRight.setVisibility(View.INVISIBLE);
			}
		}
	}

	protected TushuApplication getApp() {
		Application app = getApplication();

		return (TushuApplication) app;
	}

	protected void back() {
		finish();
	}

	/**
	 * 提示交互对话框
	 * 
	 * @param msg
	 *            提示字符串
	 */
	public void showConfirmDialog(String title, String msg, DialogInterface.OnClickListener listener) {
		showConfirmDialog(title, msg, listener, null);
	}

	public void showConfirmDialog(String title, String msg, DialogInterface.OnClickListener listener,
			DialogInterface.OnClickListener cancalListener) {
		try {
			mAlertDialogBuilder.setIcon(android.R.drawable.ic_dialog_alert).setTitle(title).setMessage(msg)
					.setPositiveButton("确定", listener).setNegativeButton("取消", cancalListener);
			mAlertDialog = mAlertDialogBuilder.create();
			mAlertDialog.setCancelable(true);
			mAlertDialog.setCanceledOnTouchOutside(true);
			mAlertDialog.show();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void showProgressBar() {
		progressDialog = ProgressDialog.show(this, "", "", true, false);
	}

	public void dismissProgressBar() {
		if (progressDialog != null && progressDialog.isShowing()) {
			try {
				progressDialog.dismiss();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected void onTopLeftClick() {}

	protected void onTopRightClick() {}

}
